/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.permit.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.market.base.permit.mapper.entity.Menu;
import net.siufung.mybatis.base.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统菜单表 Mapper 接口
 * </p>
 *
 * @author 陈建峰
 * @since 2022-07-05 22:19:50
 */
@Mapper
public interface IMenuMapper extends BaseMapper<Menu> {

    /**
     * 递归获得菜单列表
     * @param menuList 菜单递归集合
     * @param parentId 父级菜单id
     * @param allMenuList 所有父级菜单
     */
    default void getChildMenuList(List<Menu> menuList, Serializable parentId, List<Menu> allMenuList){
        if(Objects.isNull(parentId)){
            throw new BizServiceException("菜单parentId为空！");
        }
        Menu menu = this.selectById(parentId);
        if(Objects.nonNull(menu)){
            menuList.add(menu);
            List<Menu> childMenuList = allMenuList.stream()
                    .filter(element ->
//                            DeletedEnum.NOT.equals(DeletedEnum.get(element.getDeleted()))
//                                    && parentId.equals(element.getParentId()))
                            parentId.equals(element.getParentId()))
                    .collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(childMenuList)){
                for (Menu childMenu : childMenuList) {
                    getChildMenuList(menuList, childMenu.getId(), allMenuList);
                }
            }
        }
    }

    /**
     * 判断菜单名称重复
     * @param verifyRepeatDto 菜单名称
     * @return boolean
     */
    default boolean checkMenuNameRepeat(VerifyRepeatDto<Integer> verifyRepeatDto){
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0 == this.selectCount(new LambdaQueryWrapper<Menu>()
                    .eq(Menu::getMenuName, verifyRepeatDto.getVerifyKeyValue()));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0 == this.selectCount(new LambdaQueryWrapper<Menu>()
                    .eq(Menu::getMenuName, verifyRepeatDto.getVerifyKeyValue())
                    .ne(Menu::getId, verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }

    /**
     * 判断菜单路由重复
     * @param verifyRepeatDto 菜单路由
     * @return boolean
     */
    default boolean checkMenuRouterRepeat(VerifyRepeatDto<Integer> verifyRepeatDto){
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0 == this.selectCount(new LambdaQueryWrapper<Menu>()
                    .eq(Menu::getRouter, verifyRepeatDto.getVerifyKeyValue()));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0 == this.selectCount(new LambdaQueryWrapper<Menu>()
                    .eq(Menu::getRouter, verifyRepeatDto.getVerifyKeyValue())
                    .ne(Menu::getId, verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }

    /**
     * 判断菜单标识重复
     * @param verifyRepeatDto 菜单标识
     * @return boolean
     */
    default boolean checkMenuSignRepeat(VerifyRepeatDto<Integer> verifyRepeatDto){
        if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.ADD)){
            return 0 == this.selectCount(new LambdaQueryWrapper<Menu>()
                    .eq(Menu::getSign, verifyRepeatDto.getVerifyKeyValue()).isNotNull(Menu::getSign));
        } else if(verifyRepeatDto.getMethodEventEnum().equals(MethodEventEnum.UPDATE)){
            return 0 == this.selectCount(new LambdaQueryWrapper<Menu>()
                    .eq(Menu::getSign, verifyRepeatDto.getVerifyKeyValue()).isNotNull(Menu::getSign)
                    .ne(Menu::getId,  verifyRepeatDto.getPk()));
        }
        return Boolean.FALSE;
    }

}
